package com.wzy.service.impl;

import com.wzy.mapper.SysUserMapper;
import com.wzy.pojo.SysUser;
import com.wzy.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @auth wzy
 * @date 2023/10/24 14:30
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<SysUser> selectSysUserList(SysUser sysUser, Integer pageNo, Integer pageSize) {

        Integer startIndex = (pageNo-1)*pageSize;

        return sysUserMapper.selectSysUserList(sysUser,startIndex,pageSize);
    }

    @Override
    public Integer selectCount(SysUser sysUser) {
        return sysUserMapper.selectCount(sysUser);
    }

    @Override
    public int addUser(SysUser sysUser) {

        int result = sysUserMapper.add(sysUser);
        int n = 1/0;
        return result;

    }

    @Override
    public SysUser login(String account, String password) {

        SysUser sysUser = sysUserMapper.selectByAccount(account);


        if (sysUser == null){
            // 该账号不存在
            return null;
        }

        // 比较密码
        if (!sysUser.getPassword().equals(password)){
            // 密码错误
            return null;
        }

        return sysUser;
    }

    @Override
    public int add(SysUser sysUser) {
        return sysUserMapper.add(sysUser);
    }

    @Override
    public SysUser selectById(Integer id) {
        return sysUserMapper.selectById(id);
    }

    @Override
    public int update(SysUser sysUser) {
        return sysUserMapper.update(sysUser);
    }

    @Override
    public SysUser selectByAccount(String account) {

        return  sysUserMapper.selectByAccount(account);
    }
}
